package com.example.datadashboard.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.datadashboard.dto.DashboardDTO;
import com.example.datadashboard.entity.Dashboard;
import com.example.datadashboard.mapper.DashboardMapper;
import com.example.datadashboard.mapper.ReportMapper;
import com.example.datadashboard.service.DashboardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;

@Service
public class DashboardServiceImpl extends ServiceImpl<DashboardMapper, Dashboard> implements DashboardService {

    @Autowired
    private ReportMapper reportMapper;
    
    @Autowired
    private DashboardMapper dashboardMapper;

    @Override
    public DashboardDTO getDashboardData() {
        DashboardDTO dto = new DashboardDTO();
        
        // 1. 统计数据
        dto.setStats(generateStats());
        
        // 2. 图表数据
        dto.setCharts(generateChartData());
        
        // 3. 表格数据
        dto.setTable(reportMapper.selectAllReports());
        
        return dto;
    }

    private List<DashboardDTO.StatItem> generateStats() {
        List<DashboardDTO.StatItem> stats = new ArrayList<>();
        
        // 获取报告总数
        int totalReports = reportMapper.countAllReports();
        // 获取今日新增报告数
        int todayReports = reportMapper.countReportsByDate(LocalDate.now());
        
        stats.add(createStatItem("总报告数", totalReports, "file", 0));
        stats.add(createStatItem("今日新增", todayReports, "plus", 0));
        stats.add(createStatItem("完成率", 85, "check", 2.5));
        stats.add(createStatItem("平均得分", 92, "star", 1.8));
        
        return stats;
    }

    private DashboardDTO.StatItem createStatItem(String title, Number value, String icon, Number trend) {
        DashboardDTO.StatItem item = new DashboardDTO.StatItem();
        item.setTitle(title);
        item.setValue(value);
        item.setIcon(icon);
        item.setTrend(trend);
        return item;
    }

    @Override
    public List<Dashboard> getUserDashboards(Long userId) {
        return dashboardMapper.selectByUserId(userId);
    }

    @Override
    public List<Dashboard> getPublicDashboards() {
        return dashboardMapper.selectPublicDashboards();
    }

    private DashboardDTO.ChartData generateChartData() {
        DashboardDTO.ChartData chartData = new DashboardDTO.ChartData();
        
        // 生成折线图数据
        DashboardDTO.ChartItem lineChart = new DashboardDTO.ChartItem();
        lineChart.setLabels(Arrays.asList("1月", "2月", "3月", "4月", "5月", "6月"));
        
        DashboardDTO.Dataset lineDataset = new DashboardDTO.Dataset();
        lineDataset.setName("报告数量");
        lineDataset.setValues(Arrays.asList(120, 190, 170, 210, 230, 250));
        lineDataset.setColor("#409EFF");
        lineChart.setDatasets(Collections.singletonList(lineDataset));
        
        // 生成柱状图数据
        DashboardDTO.ChartItem barChart = new DashboardDTO.ChartItem();
        barChart.setLabels(Arrays.asList("周一", "周二", "周三", "周四", "周五", "周六", "周日"));
        
        DashboardDTO.Dataset barDataset = new DashboardDTO.Dataset();
        barDataset.setName("活跃用户");
        barDataset.setValues(Arrays.asList(120, 200, 150, 80, 70, 110, 130));
        barDataset.setColor("#67C23A");
        barChart.setDatasets(Collections.singletonList(barDataset));
        
        chartData.setLine(lineChart);
        chartData.setBar(barChart);
        
        return chartData;
    }
}
